home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / gnucdiff / util.c < prev   
Encoding:
C/C++ Source or Header  |  1988-12-16  |  15.1 KB  |  633 lines

  1. /* Support routines for GNU DIFF.
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU DIFF General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU DIFF, but only under the conditions described in the
  15. GNU DIFF General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU DIFF so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21. #include "diff.h"
  22.  
  23. /* Use when a system call returns non-zero status.
  24.    TEXT should normally be the file name.  */
  25.  
  26. void
  27. perror_with_name (text)
  28.      char *text;
  29. {
  30.   fprintf (stderr, "%s: %s: ", program, text);
  31.   perror (0);
  32. }
  33.  
  34. /* Use when a system call returns non-zero status and that is fatal.  */
  35.  
  36. void
  37. pfatal_with_name (text)
  38.      char *text;
  39. {
  40.   print_message_queue ();
  41.   fprintf (stderr, "%s: %s: ", program, text);
  42.   perror (0);
  43.   exit (2);
  44. }
  45.  
  46. /* Print an error message from the format-string FORMAT
  47.    with args ARG1 and ARG2.  */
  48.  
  49. void
  50. error (format, arg, arg1)
  51.      char *format;
  52.      char *arg;
  53.      char *arg1;
  54. {
  55.   fprintf (stderr, "%s: ", program);
  56.   fprintf (stderr, format, arg, arg1);
  57.   fprintf (stderr, "\n");
  58. }
  59.  
  60. /* Print an error message containing the string TEXT, then exit.  */
  61.  
  62. void
  63. fatal (message)
  64.      char *message;
  65. {
  66.   print_message_queue ();
  67.   error (message, "");
  68.   exit (2);
  69. }
  70.  
  71. /* Like printf, except if -l in effect then save the message and print later.
  72.    This is used for things like "binary files differ" and "Only in ...".  */
  73.  
  74. void
  75. message (format, arg1, arg2)
  76.      char *format, *arg1, *arg2;
  77. {
  78.   if (paginate_flag)
  79.     {
  80.       struct msg *new = (struct msg *) xmalloc (sizeof (struct msg));
  81.       if (msg_chain_end == 0)
  82.     msg_chain = msg_chain_end = new;
  83.       else
  84.     {
  85.       msg_chain_end->next = new;
  86.       msg_chain_end = new;
  87.     }
  88.       new->format = format;
  89.       new->arg1 = concat (arg1, "", "");
  90.       new->arg2 = concat (arg2, "", "");
  91.       new->next = 0;
  92.     }
  93.   else
  94.     printf (format, arg1, arg2);
  95. }
  96.  
  97. /* Output all the messages that were saved up by calls to `message'.  */
  98.  
  99. void
  100. print_message_queue ()
  101. {
  102.   struct msg *m;
  103.  
  104.   for (m = msg_chain; m; m = m->next)
  105.     printf (m->format, m->arg1, m->arg2);
  106. }
  107.  
  108. /* Call before outputting the results of comparing files NAME0 and NAME1
  109.    to set up OUTFILE, the stdio stream for the output to go to.
  110.  
  111.    Usually, OUTFILE is just stdout.  But when -l was specified
  112.    we fork off a `pr' and make OUTFILE a pipe to it.
  113.    `pr' then outputs to our stdout.  */
  114.  
  115. void
  116. setup_output (name0, name1, depth)
  117.      char *name0, *name1;
  118.      int depth;
  119. {
  120.   char *name;
  121.  
  122.   /* Construct the header of this piece of diff.  */
  123.   name = (char *) xmalloc (strlen (name0) + strlen (name1)
  124.                + strlen (switch_string) + 15);
  125.  
  126.   strcpy (name, "diff");
  127.   strcat (name, switch_string);
  128.   strcat (name, " ");
  129.   strcat (name, name0);
  130.   strcat (name, " ");
  131.   strcat (name, name1);
  132.  
  133. #if !defined(MSDOS)
  134.   if (paginate_flag)
  135.     {
  136.       int pipes[2];
  137.       int desc;
  138.  
  139.       /* For a `pr' and make OUTFILE a pipe to it.  */
  140.       pipe (pipes);
  141.  
  142.       fflush (stdout);
  143.  
  144.       desc = vfork ();
  145.       if (desc < 0)
  146.     pfatal_with_name ("vfork");
  147.  
  148.       if (desc == 0)
  149.     {
  150.       close (pipes[1]);
  151.       close (fileno (stdin));
  152.       if (dup2 (pipes[0], fileno (stdin)) < 0)
  153.         pfatal_with_name ("dup2");
  154.       close (pipes[0]);
  155.  
  156.       if (execl (PR_FILE_NAME, PR_FILE_NAME, "-f", "-h", name, 0) < 0)
  157.         pfatal_with_name (PR_FILE_NAME);
  158.     }
  159.       else
  160.     {
  161.       close (pipes[0]);
  162.       outfile = fdopen (pipes[1], "w");
  163.     } 
  164.     }
  165.   else
  166. #endif
  167.     {
  168.  
  169.       /* If -l was not specified, output the diff straight to `stdout'.  */
  170.  
  171.       outfile = stdout;
  172.  
  173.       /* If handling multiple files (because scanning a directory),
  174.      print which files the following output is about.  */
  175.       if (depth > 0)
  176.     printf ("%s\n", name);
  177.     }
  178.  
  179.   free (name);
  180. }
  181.  
  182. /* Call after the end of output of diffs for one file.
  183.    Close OUTFILE and get rid of the `pr' subfork.  */
  184.  
  185. void
  186. finish_output ()
  187. {
  188.   if (outfile != stdout)
  189.     {
  190.       fclose (outfile);
  191. #if !defined(MSDOS)
  192.       wait (0);
  193. #endif
  194.     }
  195. }
  196.  
  197. /* Compare two lines (typically one from each input file)
  198.    according to the command line options.
  199.    Each line is described by a `struct line_def'.
  200.    Return 1 if the lines differ, like `bcmp'.  */
  201.  
  202. int
  203. line_cmp (s1, s2)
  204.      struct line_def *s1, *s2;
  205. {
  206.   register char *t1, *t2;
  207.   register char end_char = line_end_char;
  208.   int savechar;
  209.  
  210.   /* Check first for exact identity.
  211.      If that is true, return 0 immediately.
  212.      This detects the common case of exact identity
  213.      faster than complete comparison would.  */
  214.  
  215.   t1 = s1->text;
  216.   t2 = s2->text;
  217.  
  218.   /* Alter the character following line 1 so it doesn't
  219.      match that following line 2.  */
  220.   savechar = s1->text[s1->length];
  221.   s1->text[s1->length] = s2->text[s2->length] + 1;
  222.  
  223.   /* Now find the first mismatch; this won't go past the
  224.      character we just changed.  */
  225.   while (*t1++ == *t2++);
  226.  
  227.   /* Undo the alteration.  */
  228.   s1->text[s1->length] = (char) savechar;
  229.  
  230.   /* If the comparison stopped at the alteration,
  231.      the two lines are identical.  */
  232.   if (t1 == s1->text + s1->length + 1)
  233.     return 0;
  234.  
  235.   /* Not exactly identical, but perhaps they match anyway
  236.      when case or whitespace is ignored.  */
  237.  
  238.   if (ignore_case_flag || ignore_space_change_flag || ignore_all_space_flag)
  239.     {
  240.       t1 = s1->text;
  241.       t2 = s2->text;
  242.  
  243.       while (1)
  244.     {
  245.       register char c1 = *t1++;
  246.       register char c2 = *t2++;
  247.  
  248.       /* Ignore horizontal whitespace if -b or -w is specified.  */
  249.  
  250.       if (ignore_all_space_flag)
  251.         {
  252.           /* For -w, just skip past any spaces or tabs.  */
  253.           while (c1 == ' ' || c1 == '\t') c1 = *t1++;
  254.           while (c2 == ' ' || c2 == '\t') c2 = *t2++;
  255.         }
  256.       else if (ignore_space_change_flag)
  257.         {
  258.           /* For -b, advance past any sequence of whitespace in line 1
  259.          and consider it just one Space, or nothing at all
  260.          if it is at the end of the line.  */
  261.           if (c1 == ' ' || c1 == '\t')
  262.         {
  263.           while (1)
  264.             {
  265.               c1 = *t1++;
  266.               if (c1 == end_char)
  267.             break;
  268.               if (c1 != ' ' && c1 != '\t')
  269.             {
  270.               --t1;
  271.               c1 = ' ';
  272.               break;
  273.             }
  274.             }
  275.         }
  276.  
  277.           /* Likewise for line 2.  */
  278.           if (c2 == ' ' || c2 == '\t')
  279.         {
  280.           while (1)
  281.             {
  282.               c2 = *t2++;
  283.               if (c2 == end_char)
  284.             break;
  285.               if (c2 != ' ' && c2 != '\t')
  286.             {
  287.               --t2;
  288.               c2 = ' ';
  289.               break;
  290.             }
  291.             }
  292.         }
  293.         }
  294.  
  295.       /* Upcase all letters if -i is specified.  */
  296.  
  297.       if (ignore_case_flag)
  298.         {
  299.           if (islower (c1))
  300.         c1 = toupper (c1);
  301.           if (islower (c2))
  302.         c2 = toupper (c2);
  303.         }
  304.  
  305.       if (c1 != c2)
  306.         break;
  307.       if (c1 == end_char)
  308.         return 0;
  309.     }
  310.     }
  311.  
  312.   return (1);
  313. }
  314.  
  315. /* Find the consecutive changes at the start of the script START.
  316.    Return the last link before the first gap.  */
  317.  
  318. struct change *
  319. find_change (start)
  320.      struct change *start;
  321. {
  322.   return start;
  323. }
  324.  
  325. struct change *
  326. find_reverse_change (start)
  327.      struct change *start;
  328. {
  329.   return start;
  330. }
  331.  
  332. /* Divide SCRIPT into pieces by calling HUNKFUN and
  333.    print each piece with PRINTFUN.
  334.    Both functions take one arg, an edit script.
  335.  
  336.    HUNKFUN is called with the tail of the script
  337.    and returns the last link that belongs together with the start
  338.    of the tail.
  339.  
  340.    PRINTFUN takes a subscript which belongs together (with a null
  341.    link at the end) and prints it.  */
  342.  
  343. void
  344. print_script (script, hunkfun, printfun)
  345.      struct change *script;
  346.      struct change * (*hunkfun) ();
  347.      void (*printfun) ();
  348. {
  349.   struct change *next = script;
  350.  
  351.   while (next)
  352.     {
  353.       struct change *this, *end;
  354.  
  355.       /* Find a set of changes that belong together.  */
  356.       this = next;
  357.       end = (*hunkfun) (next);
  358.  
  359.       /* Disconnect them from the rest of the changes,
  360.      making them a hunk, and remember the rest for next iteration.  */
  361.       next = end->link;
  362.       end->link = NULL;
  363. #ifdef DEBUG
  364.       debug_script (this);
  365. #endif
  366.  
  367.       /* Print this hunk.  */
  368.       (*printfun) (this);
  369.  
  370.       /* Reconnect the script so it will all be freed properly.  */
  371.       end->link = next;
  372.     }
  373. }
  374.  
  375. /* Print the text of a single line LINE,
  376.    flagging it with the characters in LINE_FLAG (which say whether
  377.    the line is inserted, deleted, changed, etc.).  */
  378.  
  379. void
  380. print_1_line (line_flag, line)
  381.      char *line_flag;
  382.      struct line_def *line;
  383. {
  384.   fprintf (outfile, "%s", line_flag);
  385.  
  386.   /* If -T was specified, use a Tab between the line-flag and the text.
  387.      Otherwise use a Space (as Unix diff does).
  388.      Print neither space nor tab if line-flags are empty.  */
  389.  
  390.   if (line_flag[0] != 0)
  391.     {
  392.       if (tab_align_flag)
  393.     fprintf (outfile, "\t");
  394.       else
  395.     fprintf (outfile, " ");
  396.     }
  397.  
  398.   /* Now output the contents of the line.
  399.      If -t was specified, expand tabs to spaces.
  400.      Otherwise output verbatim.  */
  401.  
  402.   if (tab_expand_flag)
  403.     {
  404.       register int column = 0;
  405.       register int i;
  406.       for (i = 0; i <= line->length; i++)
  407.     {
  408.       register char c = line->text[i];
  409.       if (c == '\t')
  410.         {
  411.           putc (' ', outfile);
  412.           column++;
  413.           while (column & 7)
  414.         {
  415.           putc (' ', outfile);
  416.           column++;
  417.         }
  418.         }
  419.       else if (c == '\b')
  420.         {
  421.           column--;
  422.           putc (c, outfile);
  423.         }
  424.       else
  425.         {
  426.           column++;
  427.           putc (c, outfile);
  428.         }
  429.     }
  430.     }
  431.   else
  432.     fwrite (line->text, sizeof (char), line->length + 1, outfile);
  433.  
  434.   if (line_end_char != '\n')
  435.     putc ('\n', outfile);
  436. }
  437.  
  438. change_letter (inserts, deletes)
  439.      int inserts, deletes;
  440. {
  441.   if (!inserts)
  442.     return 'd';
  443.   else if (!deletes)
  444.     return 'a';
  445.   else
  446.     return 'c';
  447. }
  448.  
  449. /* Translate an internal line number (an index into diff's table of lines)
  450.    into an actual line number in the input file.
  451.    The internal line number is LNUM.  FILE points to the data on the file.
  452.  
  453.    Internal line numbers count from 0 within the current chunk.
  454.    Actual line numbers count from 1 within the entire file;
  455.    in addition, they include lines ignored for comparison purposes.
  456.  
  457.    The `ltran' feature is no longer in use.  */
  458.  
  459. int
  460. translate_line_number (file, lnum)
  461.      struct file_data *file;
  462.      int lnum;
  463. {
  464.   return lnum + 1;
  465. }
  466.  
  467. void
  468. translate_range (file, a, b, aptr, bptr)
  469.      struct file_data *file;
  470.      int a, b;
  471.      int *aptr, *bptr;
  472. {
  473.   *aptr = translate_line_number (file, a - 1) + 1;
  474.   *bptr = translate_line_number (file, b + 1) - 1;
  475. }
  476.  
  477. /* Print a pair of line numbers with SEPCHAR, translated for file FILE.
  478.    If the two numbers are identical, print just one number.
  479.  
  480.    Args A and B are internal line numbers.
  481.    We print the translated (real) line numbers.  */
  482.  
  483. void
  484. print_number_range (sepchar, file, a, b)
  485.      char sepchar;
  486.      struct file_data *file;
  487.      int a, b;
  488. {
  489.   int trans_a, trans_b;
  490.   translate_range (file, a, b, &trans_a, &trans_b);
  491.  
  492.   /* Note: we can have B < A in the case of a range of no lines.
  493.      In this case, we should print the line number before the range,
  494.      which is B.  */
  495.   if (trans_b > trans_a)
  496.     fprintf (outfile, "%d%c%d", trans_a, sepchar, trans_b);
  497.   else
  498.     fprintf (outfile, "%d", trans_b);
  499. }
  500.  
  501. /* Look at a hunk of edit script and report the range of lines in each file
  502.    that it applies to.  HUNK is the start of the hunk, which is a chain
  503.    of `struct change'.  The first and last line numbers of file 0 are stored in
  504.    *FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1. 
  505.    Note that these are internal line numbers that count from 0.
  506.  
  507.    If no lines from file 0 are deleted, then FIRST0 is LAST0+1.
  508.  
  509.    Also set *DELETES nonzero if any lines of file 0 are deleted
  510.    and set *INSERTS nonzero if any lines of file 1 are inserted.
  511.    If only ignorable lines are inserted or deleted, both are
  512.    set to 0.  */
  513.  
  514. void
  515. analyze_hunk (hunk, first0, last0, first1, last1, deletes, inserts)
  516.      struct change *hunk;
  517.      int *first0, *last0, *first1, *last1;
  518.      int *deletes, *inserts;
  519. {
  520.   int f0, l0, f1, l1, show_from, show_to;
  521.   int i;
  522.   int nontrivial = !(ignore_blank_lines_flag || ignore_regexp);
  523.   struct change *next;
  524.  
  525.   show_from = show_to = 0;
  526.  
  527.   f0 = hunk->line0;
  528.   f1 = hunk->line1;
  529.  
  530.   for (next = hunk; next; next = next->link)
  531.     {
  532.       l0 = next->line0 + next->deleted - 1;
  533.       l1 = next->line1 + next->inserted - 1;
  534.       show_from += next->deleted;
  535.       show_to += next->inserted;
  536.  
  537.       for (i = next->line0; i <= l0 && ! nontrivial; i++)
  538.     if ((!ignore_blank_lines_flag || files[0].linbuf[i].length > 1)
  539.         && (!ignore_regexp
  540.         || 0 > re_search (&ignore_regexp_compiled,
  541.                   files[0].linbuf[i].text,
  542.                   files[0].linbuf[i].length, 0,
  543.                   files[0].linbuf[i].length, 0)))
  544.       nontrivial = 1;
  545.  
  546.       for (i = next->line1; i <= l1 && ! nontrivial; i++)
  547.     if ((!ignore_blank_lines_flag || files[1].linbuf[i].length > 1)
  548.         && (!ignore_regexp
  549.         || 0 > re_search (&ignore_regexp_compiled,
  550.                   files[1].linbuf[i].text,
  551.                   files[1].linbuf[i].length, 0,
  552.                   files[1].linbuf[i].length, 0)))
  553.       nontrivial = 1;
  554.     }
  555.  
  556.   *first0 = f0;
  557.   *last0 = l0;
  558.   *first1 = f1;
  559.   *last1 = l1;
  560.  
  561.   /* If all inserted or deleted lines are ignorable,
  562.      tell the caller to ignore this hunk.  */
  563.  
  564.   if (!nontrivial)
  565.     show_from = show_to = 0;
  566.  
  567.   *deletes = show_from;
  568.   *inserts = show_to;
  569. }
  570.  
  571. /* malloc a block of memory, with fatal error message if we can't do it. */
  572.  
  573. void *
  574. xmalloc (size)
  575.      unsigned size;
  576. {
  577.   register void *value = (void *) malloc (size);
  578.  
  579.   if (!value)
  580.     fatal ("virtual memory exhausted");
  581.   return value;
  582. }
  583.  
  584. /* realloc a block of memory, with fatal error message if we can't do it. */
  585.  
  586. void *
  587. xrealloc (old, size)
  588.      void *old;
  589.      unsigned int size;
  590. {
  591.   register void *value = (void *) realloc (old, size);
  592.  
  593.   if (!value)
  594.     fatal ("virtual memory exhausted");
  595.   return value;
  596. }
  597.  
  598. void *
  599. xcalloc (nitems, size)
  600.      int nitems, size;
  601. {
  602.   void *value = (void *) calloc (nitems, size);
  603.  
  604.   if (! value)
  605.     fatal ("virtual memory exhausted");
  606.   return value;
  607. }
  608.  
  609. /* Concatenate three strings, returning a newly malloc'd string.  */
  610.  
  611. char *
  612. concat (s1, s2, s3)
  613.      char *s1, *s2, *s3;
  614. {
  615.   int len = strlen (s1) + strlen (s2) + strlen (s3);
  616.   char *new = (char *) xmalloc (len + 1);
  617.   strcpy (new, s1);
  618.   strcat (new, s2);
  619.   strcat (new, s3);
  620.   return new;
  621. }
  622.  
  623. void
  624. debug_script (sp)
  625.      struct change *sp;
  626. {
  627.   fflush (stdout);
  628.   for (; sp; sp = sp->link)
  629.     fprintf (stderr, "%3d %3d delete %d insert %d\n",
  630.          sp->line0, sp->line1, sp->deleted, sp->inserted);
  631.   fflush (stderr);
  632. }
  633.